package com.gitee.xmhzzz.component.mqtt.broker.security;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @ClassName DefaultAuthSecurity
 * @Description
 * @Author wzq
 * @Date 2024/6/27 10:56
 * @Version 1.0
 */
public class DefaultAuthSecurity implements IAuthSecurity{

    private final ConcurrentMap<String/* name */, AuthInfo> authInfoTable =
            new ConcurrentHashMap<>();

    @Override
    public void addAuthSecurity(AuthInfo authInfo) {
        authInfoTable.put(authInfo.getName(),authInfo);
    }

    @Override
    public void removeAuthSecurity(String name) {
        authInfoTable.remove(name);
    }

    @Override
    public Boolean authSecurity(AuthInfo authInfo) {
        AuthInfo dbAuthInfo = authInfoTable.computeIfPresent(authInfo.getName(), (k, v) -> v);
        if (dbAuthInfo != null ){
            return authInfo.getPassword().equals(dbAuthInfo.getPassword());
        }
        return true;
    }


}
